package cn.liutao.auth.api;

import cn.hutool.core.util.StrUtil;
import cn.liutao.auth.service.UserService;
import cn.liutao.common.entity.LoginUser;
import cn.liutao.common.exception.BadRequestException;
import cn.liutao.common.utils.ServletUtils;
import cn.liutao.order.entity.vo.AlipayVo;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author: liutao
 * @description: 支付宝充值
 * @date: 2023/12/15 14:26
 */
@WebServlet("/user/exchange/alipay")
public class AlipayExchangeServlet extends HttpServlet {
    private UserService userService;
    @Override
    public void init(ServletConfig config) throws ServletException {
        userService = UserService.getInstance();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String amountStr = req.getParameter("amount");
        if (StrUtil.isBlank(amountStr)){
            throw new BadRequestException("请选择充值金额");
        }
        Integer userId = LoginUser.getUserId();
        BigDecimal amount = new BigDecimal(amountStr);
        AlipayVo alipayVo = userService.exchangeByAlipay(userId,amount);
        ServletUtils.ok(resp,alipayVo);
    }
}
